package com.LeeCode;

/**
 * 修车的最少时间
 */

public class Code2594 {
    public static void main(String[] args) {
        int[] ranks = {5, 1, 8};
        int cars = 6;
        System.out.println(new Code2594().repairCars(ranks, cars));
    }

    public long repairCars(int[] ranks, int cars) {
        long left = 0, right = Long.MAX_VALUE;
        while (left + 1 < right) {
            long mid = left + (right - left) / 2;
            if (check(ranks, mid, cars))
                right = mid;
            else
                left = mid;
        }
        return right;
    }

    private boolean check(int[] ranks, long time, int car) {
        int count = 0;
        for (int r : ranks) {
            count += (int) Math.sqrt(time / r);
            if (count >= car)
                return true;
        }
        return false;
    }
}
